package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderAndMember;
import com.itheima.service.OrderSettingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *  预约列表
 */

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderSettingListDao orderSettingListDao;

    @Autowired
    private MemberDao memberDao;

    //分页查询预约列表
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        //select * from t_order , t_member limit 0,10
        Page<OrderAndMember> page = orderSettingListDao.selectByList(queryString);
        long total = page.getTotal();
        List<OrderAndMember> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //新增预约(电话预约)
    public void add(OrderAndMember orderAndMember, Integer[] setmealIds) {
        //可能用户没有申请会员,通过电话号码查询是否是会员,是会员就就不添加会员
        String phoneNumber = orderAndMember.getPhoneNumber();
        Member member = memberDao.findByTelephone(phoneNumber);
        //查询结果是不存在,添加会员
        if (member == null) {
            //添加注册时间(当前时间)
            orderAndMember.setRegTime(new Date());
            //档案号(以时间毫秒值来定,后期在确定改正)
            orderAndMember.setFileNumber(new Date().getTime()+"1");
            //初始密码
            orderAndMember.setPassword("1234");
            //添加会员
            orderSettingListDao.addOrderAndMember(orderAndMember); //添加新的会员,member表
        }

        //重新查电话号码取出会员id(会员存在)
        Member members = memberDao.findByTelephone(phoneNumber);
        Integer memberId = members.getId();   //能查出添加会员后的id
        orderAndMember.setMemberId(memberId);//将会员id添加到实体类中
        //初始化到诊状况
        orderAndMember.setOrderStatus(orderAndMember.ORDERSTATUS_NO);
        if (setmealIds != null) {
            //遍历套餐信息添加到OrderAndMember中添加数据
            for (Integer setmealId : setmealIds) {
                //套餐id添加
                orderAndMember.setSetmealId(setmealId);
                orderSettingListDao.add(orderAndMember);
            }
        }
    }

    //根据order主键id单一/批量删除
    public void delete(Integer[] orderIds) {

        if (orderIds != null) {
            for (Integer orderId : orderIds) {
                orderSettingListDao.deleteById(orderId);
            }
        }
    }
}
